﻿using System;
using Missbot.Service;

namespace Missbot.AIModule.Pet
{
    /// <summary>
    /// 宠物耐力记录
    /// </summary>
    public class PetEnduranceRecord : DbBaseEntity
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserID { get; init; }

        /// <summary>
        /// 日期字符串
        /// </summary>
        public string DateStr { get; set; }

        /// <summary>
        /// 耐力总消耗
        /// </summary>
        public int ConsumeTotal { get; set; }
    }

    public static class PetEnduranceDA
    {
        /// <summary>
        /// 获取宠物的当日耐力消耗记录
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static PetEnduranceRecord Get(string UserID)
        {
            var record = MongoService<PetEnduranceRecord>.GetOnly(p => p.UserID == UserID);
            if (record == null)
            {
                record = new PetEnduranceRecord { UserID = UserID };
                MongoService<PetEnduranceRecord>.Insert(record);
            }

            var todayStr = DateTime.Now.ToString("yyyyMMdd");
            if (record.DateStr == todayStr)
            {
                return record;
            }

            record.ConsumeTotal = 0;
            record.DateStr = todayStr;

            return record;
        }

        /// <summary>
        /// 更新
        /// </summary>
        public static void Update(this PetEnduranceRecord entity)
        {
            MongoService<PetEnduranceRecord>.Update(entity);
        }
    }
}